package com.qcu.controller.user;

import com.qcu.annotation.LimitRate;
import com.qcu.annotation.RequireAuthorization;
import com.qcu.entity.Appointment;
import com.qcu.entity.AppointmentTemp;
import com.qcu.exception.RepeatException;
import com.qcu.service.AppointmentService;
import com.qcu.service.AppointmentTempService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import com.qcu.vo.AppointmentInfoVO;
import com.qcu.vo.DepartmentVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user/appointment")
public class AppointmentController {
    @Resource
    AppointmentService appointmentService;
    @Resource
    AppointmentTempService appointmentTempService;

    @GetMapping("/getDepartmentWithDoctor")
    @LimitRate(key = "getDepartmentWithDoctor",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"USER"})
    public Response getDepartmentWithDoctor() {
        List<DepartmentVO> departmentWithDoctor = appointmentService.getDepartmentWithDoctor();
        return Response.success(departmentWithDoctor);
    }
    @PostMapping("/save")
    @RequireAuthorization({"USER"})
    public Response save(@RequestBody AppointmentTemp appointmentTemp) {
        try {
            appointmentTemp.setState(0);
            boolean save = appointmentTempService.save(appointmentTemp);
            if (save) {
                return Response.success("预约成功");
            } else {
                return Response.failure(ResponseCode.FAILURE);
            }
        } catch (Exception e) {
            throw new RepeatException(e.getMessage());
        }
    }
    @GetMapping("/get")
    @LimitRate(key = "getAppointment",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    @RequireAuthorization({"USER"})
    public Response get() {
        List<AppointmentInfoVO> information = appointmentService.getInformationToUser();
        return Response.success(information);
    }
}